/**
 * demo.js
 * http://www.codrops.com
 *
 * Licensed under the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 * 
 * Copyright 2016, Codrops
 * http://www.codrops.com
 */

// 'use strict';

// taken from mo.js demos
function isIOSSafari() {
	var userAgent;
	userAgent = window.navigator.userAgent;
	return userAgent.match(/iPad/i) || userAgent.match(/iPhone/i);
};

// taken from mo.js demos
function isTouch() {
	var isIETouch;
	isIETouch = navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0;
	return [].indexOf.call(window, 'ontouchstart') >= 0 || isIETouch;
};

// taken from mo.js demos
var isIOS = isIOSSafari(),
	clickHandler = isIOS || isTouch() ? 'touchstart' : 'click';

function mo_extend(a, b) {
	for (var key in b) {
		if (b.hasOwnProperty(key)) {
			a[key] = b[key];
		}
	}
	return a;
}

function Animocon(el, options) {
	this.el = el;
	this.options = mo_extend({}, this.options);
	mo_extend(this.options, options);

	// this.checked = false;

	this.timeline = new mojs.Timeline();

	for (var i = 0, len = this.options.tweens.length; i < len; ++i) {
		this.timeline.add(this.options.tweens[i]);
	}

	var self = this;
	this.el.on(clickHandler, function () {
		if (el.hasClass('recommed')) {
			self.options.onUnCheck();
		} else {
			self.options.onCheck();
			self.timeline.replay();
		}
	});

}

Animocon.prototype.options = {
	onCheck: function () { return false; },
	tweens: [
		new mojs.Burst({})
	],

	onUnCheck: function () { return false; }
};

// grid items:
// var items = [].slice.call(document.querySelectorAll('.like-icon'));

function mo_init(el, url1, url2) {
	var likeIcon_b = jQuery(el).find('i'),
		g_fid = jQuery(el).data('groupfid'),
		g_tid = jQuery(el).data('grouptid'),
		like_add = jQuery(el).siblings('.like-num');
	scaleCurve6 = mojs.easing.path('M0,100 L25,99.9999983 C26.2328835,75.0708847 19.7847843,0 100,0');
	new Animocon(el, {
		tweens: [
			// burst animation
			new mojs.Burst({
				parent: el,
				radius: { 8: 20 },
				count: 20,
				children: {
					shape: 'line',
					fill: 'white',
					radius: { 20: 0 },
					scale: 1,
					stroke: '#EE9393',
					strokeWidth: 1,
					duration: 800,
					easing: mojs.easing.bezier(0.1, 1, 0.3, 1)
				}
			}),
			// ring animation
			new mojs.Shape({
				parent: el,
				radius: { 8: 15 },
				fill: 'transparent',
				stroke: '#EE9393',
				strokeWidth: { 10: 0 },
				duration: 800,
				easing: mojs.easing.bezier(0.3, 1, 0.3, 0.9)
			}),
			// icon scale animation
			new mojs.Tween({
				duration: 800,
				easing: mojs.easing.bezier(0.1, 1, 0.3, 1),
				onUpdate: function (progress) {
					var scaleProgress = scaleCurve6(progress);
					likeIcon_b.css('transform', 'scale3d(' + progress + ',' + progress + ',1)');
					var trans = likeIcon_b.css('transform');
					likeIcon_b.css('WebkitTransform', trans)
				}
			})
		],
		onCheck: function (e) {
			// jQuery(el).data( 'like_flag', new Date().getTime() );
				window.event? window.event.cancelBubble = true : e.stopPropagation();
				window.event? window.event.returnValue = false : e.preventDefault();
				likeIcon_b.css('color', '#EE9393');
				jQuery(el).addClass('recommed');
				var like_num = Number(like_add.text());
				console.log(like_num)
				like_add.text(like_num + 1);
			
			// jQuery.ajax({
			// 	url: url1,
			// 	data: {
			// 		tid: g_tid,
			// 		fid: g_fid
			// 	},
			// 	dataType: 'json',
			// 	success: function(resp){
			// 		if( resp['returnData']['result'] == 1 ){
			// 			likeIcon_b.css('color', '#EE9393');
			// 			jQuery(el).addClass('recommed');
			// 			var like_num = Number( like_add.text() );
			// 			like_add.text( like_num + 1 );
			// 		}else {
			// 			alert( resp['returnData']['message'] );
			// 		} 


			// 	}
			// })

		},
		onUnCheck: function (e) {
			window.event? window.event.cancelBubble = true : e.stopPropagation();
			window.event? window.event.returnValue = false : e.preventDefault();
			// jQuery(el).data( 'like_flag', new Date().getTime() );
			console.log("取消点赞")
		
				likeIcon_b.css('color', '#c9d6de');
				jQuery(el).removeClass('recommed');
				var like_num = Number(like_add.text());
				like_add.text(like_num - 1);
			// 
			// jQuery.ajax({
			// 	url: url2,
			// 	data: {
			// 		tid: g_tid
			// 	},
			// 	dataType: 'json',
			// 	success: function(resp){ 
			// 		if( resp['returnData']['result'] == 1 ){
			// 			likeIcon_b.css('color', '#c9d6de');
			// 			jQuery(el).removeClass('recommed');
			// 			var like_num = Number( like_add.text() );
			// 			like_add.text( like_num - 1 );
			// 		}else {
			// 			alert( resp['returnData']['message'] );
			// 		}
			// 	}
			// })
		}
	});
}


